# -*- coding: UTF-8 -*-
"""
飞行数据导入画图
"""

import matplotlib.pyplot as plt
# import numpy as np

# 画图显示中文需要加此两行
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


class MyData(dict):
    """数据解析画图类"""

    def __init__(self, *file_name):
        """构造函数, 初始化一个 MyData 对象"""
        super().__init__()
        if file_name:
            self.import_file(file_name[0])

    def import_file(self, file_name):
        """从文本文件导入数据"""
        if not isinstance(file_name, str):
            raise TypeError('illegal file name!')
        with open(file_name, 'r') as data_file:
            name_list = data_file.readline().strip(' ').strip('\n').split()
            for name in name_list:
                self[name] = []
            for data_line_string in data_file:
                data_list = list(
                    map(float,
                        data_line_string.strip(' ').strip('\n').split()))
                for name in name_list:
                    self[name].append(data_list[name_list.index(name)])

    def plot(self, *data_name):
        """数据绘图"""
        fig = plt.figure()
        my_axis = fig.add_subplot(111)
        for value in data_name:
            if value in self:
                my_axis.plot(self[value], label=value)
        my_axis.legend()
        my_axis.grid()


# 导入数据 画图
MY_DATA = MyData('data.txt')
MY_DATA.plot('m1', 'm2', 'm3', 'm4', 'm5', 'm6')
MY_DATA.plot('rc1', 'rc2', 'rc3', 'rc4')
MY_DATA.plot('xacc', 'yacc', 'zacc')
MY_DATA.plot('roll', 'ygyro')
MY_DATA.plot('pitch', 'xgyro')

plt.show()
# 打印所有的数据名
print(MY_DATA.keys())
